package com.venusz.datasource.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 动态数据源上下文管理
 * Created with IntelliJ IDEA
 * Created By Kinsey
 * Date: 2018/7/17
 * Time: 22:42
 */
public class DynamicDataSourceContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    private Logger logger = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);

    /*
     * 管理所有的数据源id;
     * 主要是为了判断数据源是否存在;
     */
    public static List<String> dataSourceIds = new ArrayList<String>();

    public static void setDataSourceType(String sourceType){
        contextHolder.set(sourceType);
    }

    public static String getDataSourceType(){
        return contextHolder.get();
    }

    public static void cleanDataSourceType(){
        contextHolder.remove();
    }

    public static boolean containsDataSource(String dataSourceId){
        return dataSourceIds.contains(dataSourceId);
    }
}
